/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "zlang_in.h"

struct ZlangInterface *CreateInterface( struct ZlangRuntime *rt , char *interface_name )
{
	struct ZlangInterface	*interface = NULL ;
	int			nret = 0 ;
	
	interface = (struct ZlangInterface *)ZLMALLOC( sizeof(struct ZlangInterface) ) ;
	if( interface == NULL )
		return NULL;
	memset( interface , 0x00 , sizeof(struct ZlangInterface) );
	
	interface->inner = AllocObject( rt ) ;
	if( interface->inner == NULL )
	{
		DestroyInterface( rt , interface );
		return NULL;
	}
	memset( interface->inner , 0x00 , sizeof(struct ZlangObject) );
	
	nret = InitObject( rt , interface->inner , interface_name , NULL , 0 ) ;
	if( nret )
	{
		DestroyInterface( rt , interface );
		return NULL;
	}
	
	return interface;
}

void DestroyInterface( struct ZlangRuntime *rt , struct ZlangInterface *interface )
{
	if( interface )
	{
		if( interface->inner )
		{
			DestroyObject( rt , interface->inner );
		}
		
		ZLFREE( interface );
	}
	
	return;
}

struct ZlangInterface *QueryGlobalInterfaceByInterfaceName( struct ZlangRuntime *rt , char *interface_name )
{
	struct ZlangInterface	i ;
	
	memset( & i , 0x00 , sizeof(struct ZlangInterface) );
	i.interface_name = interface_name ;
	return QueryInterfaceInRuntimeInterfacesHeapByInterfaceName( rt , & i );
}

char *GetInterfaceName( struct ZlangInterface *interface )
{
	if( interface == NULL )
		return NULL;
	
	return interface->interface_name;
}

struct ZlangObject *GetInterfaceInner( struct ZlangInterface *interface )
{
	if( interface == NULL )
		return NULL;
	
	return interface->inner;
}

void DebugPrintInterface( struct ZlangRuntime *rt , struct ZlangInterface *interface )
{
	if( interface == NULL )
		return;
	
	PRINT_TABS(rt) printf( " interface[%p][%s] " , interface , GetObjectName(interface->inner) ); DebugPrintObject(rt,interface->inner); PRINT_SOURCE_FILE_LINE PRINT_NEWLINE
	
	DebugPrintObjectFunctions( rt , interface->inner );
	
	return;
}

